<!-- 
function page 
-->
<html>
<head>
<title>${description!} ${functionName} ${interfaceName} 接口方法</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF8" />
</head>
<body>
    <h1>接口方法</h1>
    <a href="?from=${interfaceName}.${functionName}">Index</a> >
    <a href="?interface=${interfaceName}">${interfaceName}</a> >
    <a>${functionName}</a>
    <br />

    <h3>说明: ${description!"描述"}</h3>

    <#if deprecated>
    <h4>不建议使用此接口 不建议使用此接口</h4>
    </#if>

    <h4>InterfaceName: ${interfaceName}</h4>
    <h4>MethodName: ${functionName}</h4>
    <h4>ReturnType: ${returnTypeName}</h4>

    <#if signCheck || authenticationCheck>
    <table border="1">
        <#if signCheck>
        <tr>
            <td>_app_key <b><a href="?action=sign_check" title="这是什么?">[?]</a></b></td>
            <td><b>必须</b></td>
            <td><input id="_app_key" size="88" /></td>
        </tr>
        <tr>
            <td>_app_secret</td>
            <td><b>必须</b></td>
            <td><input id="_app_secret" size="88" /></td>
        </tr>
        </#if>

        <tr>
            <td>_token</td>
            <td><#if authenticationCheck> <b>必须</b> <#else><span title="可传可不传">非必须</span></#if></td>
            <td><input id="_token" size="88" /></td>
        </tr>
    </table>
    </#if>

    <h3>parameters :</h3>
    <table id="parameters" border="1">
        <#list parameterTypeNames as parameterTypeName>
        <tr>
            <td class="parameterName" isEnum="${parameterTypeIsEnum[parameterTypeName_index]?c}">
            <#if parameterNames??>
                ${parameterNames[parameterTypeName_index]}
            <#else>
                param_${parameterTypeName_index}
            </#if>
            </td>
            <td>${parameterDescription[parameterTypeName_index] ! "参数"+parameterTypeName_index}</td>
            <td>${nullAble[parameterTypeName_index]?string("可选","不可为空")}</td>
            <td class="parameterType">${parameterTypeName}</td>
            <td class="argumentValue">
                <textarea rows="1" cols="88"></textarea>
            </td>
        </tr>
        </#list>
    </table>
    <br />

<#if parameterNames??>
    <button id="do_sdk">发送请求</button>
    <br />
    <br />
    <br />
    <span style="font-size: 20px; font-weight: 900;">JavaScript Sdk 调用示例</span>
    <a target="_blank" href="?action=javascript-sdk">下载JavaScript Sdk</a>
    <pre>
    sdk.${interfaceName}().${functionName}({
    <#list method.parameterTypes as parameterType>
        ${parameterNames[parameterType_index]}:<#if "String"==parameterType.simpleName || "Integer"==parameterType.simpleName || "Long"==parameterType.simpleName>""<#else>{} // ${parameterType.simpleName} 对象 </#if> <#if parameterType_index lt (method.parameterTypes?size-1)>, </#if>
    </#list>
    }).then(function(result) {
            alert("success");
        }
    );
    </pre>
<#else>
	<h1>no parameterNames ?</h1>
</#if>

    <script src="//cdn.staticfile.org/jquery/1.12.4/jquery.min.js"></script>
    <script src="//cdn.staticfile.org/blueimp-md5/2.10.0/js/md5.min.js"></script>
    <script src="${request_uri_1}?action=javascript-sdk-download&scope=all"></script>
    <script type="text/javascript">
    // 初始化SDK
    var sdk = new BudoDubboHttpApiJavascriptSdk();
    var defaultDeferredFactory =  sdk._deferredFactory;

    sdk.setDeferredFactory(function (_request, _sdk){
        return new Promise(function(resolve, reject){
            defaultDeferredFactory(_request, _sdk).then(resolve, reject);
        });
    });

    // 传入一个 RequestFilter
    sdk.setRequestFilter(function(_request){
        var _token  = $("#_token").val();

        if(_token){
            _request.parameters["_token"] = _token;
        }

        var _app_key = $("#_app_key").val();
        if(_app_key){
            var _app_secret = $("#_app_secret").val();

            var _time = (new Date()).getTime();
            var _request_body = "appSecret=" + _app_secret+"&requestBody=" + _request.requestBody +  "&time=" + _time ;
            var _sign = md5(_request_body);

            _request.parameters["_app_key"] = _app_key;
            _request.parameters["_time"] = _time;
            _request.parameters["_sign"] = _sign;
        }

        return _request;
    });

    $(function(){
        $("#_token").val(localStorage.getItem("_token"));
        $("#_app_key").val(localStorage.getItem("_app_key"));
        $("#_app_secret").val(localStorage.getItem("_app_secret"));

        $("#do_sdk").on("click", function(evevt) {
            localStorage.setItem("_token", $("#_token").val());

            localStorage.setItem("_app_key", $("#_app_key").val());
            localStorage.setItem("_app_secret", $("#_app_secret").val());

            var argumentsError = false;
            var _arguments = {};

            $("#parameters tr").each(function(index, element) {
                var parameterName = $(element).find(".parameterName").html().trim();
                var isEnum = $(element).find(".parameterName").attr("isEnum");
                var parameterType = $(element).find(".parameterType").html().trim();
                var argumentValue = $(element).find(".argumentValue textarea").val();

                if (null == argumentValue || undefined == argumentValue || '' == argumentValue ) { 
                    // 值为空
                } else if ("String" == parameterType || "Timestamp" == parameterType || "Date" == parameterType) {
                    _arguments[parameterName] = argumentValue + "";
                } else if ("Integer" == parameterType || "int" == parameterType) {
                    _arguments[parameterName] = parseInt(argumentValue);
                } else if ("Boolean" == parameterType || "boolean" == parameterType) {
                    _arguments[parameterName] = "true" == argumentValue + "" || "1" == argumentValue + "";
                } else if ("" != argumentValue) {
                    try {
                        _arguments[parameterName] = JSON.parse(argumentValue);
                    } catch (e) {
                        alert("JSON.parse error, e="+e+", value=" + argumentValue + ", parameterName=" + parameterName);
                        argumentsError = true;
                        return false; // break
                    }
                } else {
                    _arguments[parameterName] = null;
                }
            });

            if (!argumentsError) {
                sdk.${interfaceName}().${functionName}(_arguments).then(function(_result, _request) {
                    console.log(_request);
                    alert("自定义的 success，result=" + JSON.stringify(_result));
                }, function(_error, _request) {
                    console.log(_request);
                    alert("自定义的 fail，error=" + JSON.stringify(_error));
                });
            }
        });
   });
</script>

</body>
</html>